package com.ylbh.modules.business.controller;

import com.ylbh.common.annotation.SysLog;
import com.ylbh.common.exception.NormalException;
import com.ylbh.common.pojo.business.dto.BusinessDetailsDTO;
import com.ylbh.common.pojo.business.dto.CashWithdrawalDTO;
import com.ylbh.common.pojo.business.dto.NoAdoptDTO;
import com.ylbh.common.pojo.business.dto.ToExamineDTO;
import com.ylbh.common.pojo.business.vo.CashWithdrawalDetailsVO;
import com.ylbh.common.pojo.business.vo.ToExamineDetailsVO;
import com.ylbh.common.pojo.userclass.dto.BusinessInformationDTO;
import com.ylbh.common.pojo.userclass.dto.UpdateBusinessInformationDTO;
import com.ylbh.common.pojo.userclass.vo.BusinessDetailsVO;
import com.ylbh.common.utils.*;
import com.ylbh.modules.business.entity.ShoppingUser;
import com.ylbh.modules.business.service.ShoppingUserService;
import com.ylbh.modules.userclass.service.ShoppingUserClassService;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;


/**
 *
 * @author czx
 * @date 2019-04-15 15:35:45
 */
@RestController
@RequestMapping("user/business")
public class ShoppingUserController {

    @Autowired
    private ShoppingUserService shoppingUserService;

    @Autowired
    private ShoppingUserClassService shoppingUserClassService;

    @ApiOperation("联盟店审核列表")
    @RequestMapping("/queryBusinessList")
    @RequiresPermissions("user:business:queryBusinessList")
    public CommonResult<PageUtils> queryBusinessList(ToExamineDTO params){
        PageUtils pageUtils = shoppingUserService.queryBusinessList(params);
        return Ref.ok(pageUtils);
    }

    @ApiOperation("联盟店未审核数量")
    @RequestMapping("/queryCount")
    public CommonResult<Integer> queryCount() {
        int count = shoppingUserService.queryCount();
        return Ref.ok(count);
    }

    @SysLog("允许联盟店通过审核")
    @ApiOperation("允许联盟店通过审核")
    @RequestMapping("/modifyStatusById")
    @RequiresPermissions("user:business:modifyStatusById")
    public CommonResult<Void> modifyStatusById(@RequestBody Long id){
        shoppingUserService.modifyStatusById(id);
        return Ref.ok();
    }

    @SysLog("不允许联盟店通过审核")
    @ApiOperation("不允许联盟店通过审核")
    @RequestMapping("/modifyStatusByIds")
    @RequiresPermissions("user:business:modifyStatusByIds")
    public CommonResult<Void> modifyStatusByIds(@RequestBody NoAdoptDTO noAdopts){
        // 根据联盟店的id对用户进行审核操作
        shoppingUserService.modifyStatusByIds(noAdopts);
        return Ref.ok();
    }

    @SysLog("批量审核")
    @ApiOperation("批量审核")
    @RequestMapping("/modifyBatchByIds/{batch}")
    @RequiresPermissions("user:business:modifyBatchByIds")
    public CommonResult<Void> modifyBatchByIds(@RequestBody String[] ids, @PathVariable Integer batch){
        shoppingUserService.modifyBatchByIds(Arrays.asList(ids), batch);
        return Ref.ok();
    }

    @SysLog("批量删除")
    @ApiOperation("批量删除")
    @RequestMapping("/removeBatchByIds")
    @RequiresPermissions("user:business:removeBatchByIds")
    public CommonResult<Void> removeBatchByIds(@RequestBody String[] ids){
        shoppingUserService.removeBatchByIds(Arrays.asList(ids));
        return Ref.ok();
    }

    @ApiOperation("根据id查询联盟店的详情")
    @RequestMapping("/getBusinessUserById/{id}")
    @RequiresPermissions("user:business:getBusinessUserById")
    public CommonResult<ToExamineDetailsVO> getBusinessUserById(@PathVariable("id")Long id){
        ToExamineDetailsVO byId = shoppingUserService.getBusinessUserById(id);
        return Ref.ok(byId);
    }

    @SysLog("根据id修改联盟店审核的详情")
    @ApiOperation("根据id修改联盟店审核的详情")
    @RequestMapping("/modifyBusinessUserById")
    @RequiresPermissions("user:business:modifyBusinessUserById")
    public CommonResult<Void> modifyBusinessUserById(@RequestBody BusinessDetailsDTO businessDetailsDTO) {
        shoppingUserService.modifyBusinessUserById(businessDetailsDTO);
        return Ref.ok();
    }

    @ApiOperation("上传会员证件图片")
    @RequestMapping("/uploadImg")
    public CommonResult<String> uploadImg(@RequestBody MultipartFile file){
        if(file==null){
            return Ref.error("上传图片时，图片为空");
        }
        OSSClientUtil ossClient = null;
        try{
            ossClient = new OSSClientUtil();
            String imgUrl = ossClient.uploadImg2Oss(file,"business");//上传文件到服务器
            if("fail".equals(imgUrl)){
                return Ref.error("上传失败");
            }
            return Ref.ok(imgUrl);
        }catch (Exception e) {									//捕获异常
            e.printStackTrace();
            return Ref.error("上传失败");
        }
    }

    @ApiOperation("联盟店信息管理列表")
    @RequestMapping("/queryBusinessInformation")
    @RequiresPermissions("business:user:queryBusinessInformation")
    public CommonResult<PageUtils> queryBusinessInformation(BusinessInformationDTO businessInformationDTO){
        PageUtils list = shoppingUserClassService.queryBusinessInformation(businessInformationDTO);
        return Ref.ok(list);
    }

    @ApiOperation("根据id查看联盟店信息管理详情信息")
    @RequestMapping("/getBusinessInformationById/{id}")
    @RequiresPermissions("business:user:getBusinessInformationById")
    public CommonResult<BusinessDetailsVO> getBusinessInformationById(@PathVariable Long id){
        BusinessDetailsVO vo = shoppingUserClassService.getBusinessInformationById(id);
        return Ref.ok(vo);
    }

    @SysLog("根据id修改联盟店信息管理详情信息")
    @ApiOperation("根据id修改联盟店信息管理详情信息")
    @RequestMapping("/modifyClassName")
    @RequiresPermissions("business:user:modifyClassName")
    public CommonResult<Void> modifyClassName(@RequestBody UpdateBusinessInformationDTO updateByIdOrInformationDTO){
        shoppingUserClassService.modifyClassName(updateByIdOrInformationDTO);
        return Ref.ok();
    }

    @SysLog("导出联盟店信息管理")
    @ApiOperation("导出联盟店信息管理")
    @RequestMapping("businessInformationExcel")
    @RequiresPermissions("business:user:businessInformationExcel")
    public CommonResult<Void> businessInformationExcel(HttpServletResponse response, HttpServletRequest request) throws NormalException {
        shoppingUserClassService.businessInformationExcel(response ,request);
        return Ref.ok();
    }

    @ApiOperation("联盟店提现")
    @RequestMapping("/queryBusinessCashWithdrawal")
    @RequiresPermissions("business:user:queryBusinessCashWithdrawal")
    public CommonResult<PageUtils> queryBusinessCashWithdrawal(CashWithdrawalDTO cashWithdrawalDTO){
        PageUtils withdrawal = shoppingUserClassService.queryBusinessCashWithdrawal(cashWithdrawalDTO);
        return Ref.ok(withdrawal);
    }

    @ApiOperation("根据id查看联盟店提现详情")
    @RequestMapping("/getBusinessCashWithdrawal/{id}")
    @RequiresPermissions("business:user:getBusinessCashWithdrawal")
    public CommonResult<CashWithdrawalDetailsVO> getBusinessCashWithdrawal(@PathVariable(value = "id")Long id){
        CashWithdrawalDetailsVO find = shoppingUserClassService.getBusinessCashWithdrawal(id);
        return Ref.ok(find);
    }

    @SysLog("联盟店重新审核")
    @ApiOperation("联盟店重新审核")
    @RequestMapping("modifyReExamination/{id}/{status}")
    public CommonResult<Void> modifyReExamination(@PathVariable Long id, @PathVariable Integer status) {
        ShoppingUser user = new ShoppingUser();
        user.setId(id);
        user.setStatus(status);
        shoppingUserService.updateById(user);
        return Ref.ok();
    }

    @SysLog("修改联盟店的满送类型")
    @ApiOperation("修改联盟店的满送类型")
    @PostMapping("modifyPayFullSendById/{id}/{payFullSendType}")
    public CommonResult<String> modifyPayFullSendById(@PathVariable Long id, @PathVariable Integer payFullSendType) {
        String msg = shoppingUserService.modifyPayFullSendById(id, payFullSendType);
        return Ref.ok(msg);
    }

}
